﻿using CapturePackage.DataService;
using CapturePackage.Model;
using CapturePackage.Utility;
using HtmlAgilityPack;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace CapturePackage
{
    public class CommoditySearch : ISearch
    {
        /// <summary>
        /// xpath抓包
        /// </summary>
        private Category category = null;

        public CommoditySearch(Category _category)
        {
            category = _category;
        }

        public CommoditySearch()
        {
            //category = _category;
        }

        /// <summary>
        /// ？？？问题 ，直接用node，为啥是同一个数据
        /// </summary>
        /// <param name="node"></param>
        private void FindCommoditySingle(HtmlNode node, ProductMessage productMessage)//可以直接传递node
        {
            HtmlDocument htmlDocument = new HtmlDocument();
            htmlDocument.LoadHtml(node.OuterHtml);
            node = htmlDocument.DocumentNode;
            {
                string namePath = "//div[@class='gl-i-wrap']//div[contains(@class,'p-name')]/a/em";
                HtmlNode nameNode = node.SelectSingleNode(namePath);
                string name = nameNode.InnerText.Trim();
                productMessage.Title = name;

                string qj = "//li[@class='gl-item']//div[@class='p-shop']//a[@class='curr-shop hd-shopname']";
                HtmlNode qjNode = node.SelectSingleNode(qj);
                if (qjNode != null && qjNode.InnerText != null)
                    productMessage.ConfluenceSpaceId = qjNode.InnerText.Trim();
                productMessage.DateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                string urlPath = "//div[@class='gl-i-wrap']//div[contains(@class,'p-name')]/a";
                HtmlNode urlNode = node.SelectSingleNode(urlPath);
                string url = $"https:{urlNode.Attributes["href"].Value}";
                productMessage.href = url;

                string picturePath = "//div[@class='gl-i-wrap']//div[contains(@class,'p-img')]/a/img";
                HtmlNode pictureNode = node.SelectSingleNode(picturePath);

                string pictureUrl = null;
                if (pictureNode == null)
                {
                    Console.WriteLine("图片节点为空");
                }
                else if (pictureNode.Attributes["src"] == null)
                {
                    //延迟-惰性加载，其实是js框架去完成的，
                    //一开始只展示电脑屏幕可见的图片，其他图片不加载的---提升性能
                    //滚动页面，js完成新的图片加载，就是把url绑定到src属性，浏览器就会自动加载
                    pictureUrl = $"https:{pictureNode.Attributes["data-lazy-img"].Value}";
                    //Console.WriteLine("图片节点src为空");
                }
                //所见非所得
                else
                {
                    pictureUrl = $"https:{pictureNode.Attributes["src"].Value}";
                }
                productMessage.ImageUrl = pictureUrl;

                string pricePath = "//div[@class='gl-i-wrap']//div[contains(@class,'p-price')]/strong/i";
                HtmlNode priceNode = node.SelectSingleNode(pricePath);
                if (priceNode == null)
                {
                    Console.WriteLine("价格节点为空");
                }
                else
                {
                    string sPrice = priceNode.InnerText;
                    //价格自然是动态获取+绑定，ajax请求
                    //ajax：js发起的一个基于XmlHttpRequest，
                    productMessage.Price = sPrice;
                }
                //https://item.jd.com/5436676.html
                string skuId = url.Substring(url.LastIndexOf('/') + 1).Replace(".html", "");
            }
        }

        public void Crawler()
        {
            try
            {
                if (string.IsNullOrEmpty(category.Url))
                {
                    //warnRepository.SaveWarn(category, string.Format("Url为空,Name={0} Level={1} Url={2}", category.Name, category.CategoryLevel, category.Url));
                    return;
                }
                category.Url = "https://www.chnmuseum.cn/zp/zpml/gjwxbt/index.shtml";

                string html = HttpHelper.DownloadUrl(category.Url);//下载html
                {
                    int pageCount = 0;
                    #region 抓取京东商品信息
                    {
                        try
                        {

                            HtmlDocument htmlDocument = new HtmlDocument();
                            htmlDocument.LoadHtml(html);
                            //抓取数据总页数
                            string pagePath = @"//div[@class='cj_ercom_wai cj_bg_write cj_vio_padd3']//div[@class='cj_page2']";
                            HtmlNode htmlNode = htmlDocument.DocumentNode.SelectSingleNode(pagePath);
                            pageCount = int.Parse(htmlNode.InnerText);

                            //获取数据的详细信息
                            for (int i = 0; i < pageCount; i++)
                            {
                                if (i != 0)
                                {
                                    string url = $"https://search.jd.com/Search?keyword=%E7%9C%BC%E9%95%9C&wq=%E7%9C%BC%E9%95%9C&pvid=4a7c60b665d44665a951befc74e975e7&isList=0&page={i + 1}&s=58&click=0&log_id=1725420019954.8660";
                                    html = HttpHelper.DownloadUrl(url);//下载html
                                    htmlDocument.LoadHtml(html);
                                }
                                HtmlNodeCollection innerContext = htmlDocument.DocumentNode.SelectNodes(@"//div[@id='J_goodsList']//ul[@class='gl-warp clearfix']/li");
                                //获取抓取的数据
                                List<ProductMessage> productMessages = new List<ProductMessage>();
                                if (innerContext == null)
                                    continue;
                                foreach (var item in innerContext)
                                {
                                    ProductMessage productMessage = new ProductMessage();
                                    FindCommoditySingle(item, productMessage);
                                    productMessages.Add(productMessage);
                                }
                                SqlSugarClient sqlSugarClient = SqlHelper.GetSqlSugarClient();
                                sqlSugarClient.Insertable<ProductMessage>(productMessages).ExecuteCommand();
                            }
                        }
                        catch (Exception ex)
                        {

                        }
                    }
                    #endregion

                    #region 单页抓取
                    //{
                    //    string path = @"//div[@class='project list-warpper']//span[@class='pro-name']/a[2]";
                    //    HtmlNode node = document.DocumentNode.SelectSingleNode(path);
                    //    string title = node.Attributes["title"].Value;
                    //}
                    //{
                    //    string path = "//div[@class='project list-warpper']//span[@class='pro-name']/a[2]";
                    //    HtmlNodeCollection nodeCollection = document.DocumentNode.SelectNodes(path);
                    //    foreach (var node in nodeCollection)
                    //    {
                    //        Console.WriteLine(node.InnerText);
                    //    }
                    //}
                    #endregion

                    #region 多页抓取
                    {
                        ////分页：分析--->尝试---->测试--->分析--->尝试---->测试
                        //string rootUrl = category.Url;
                        ////1 找总页数---超出后是展示的最后一页
                        //{
                        //    string totalPagePath = @"//div[@class='Price--priceWrapper--Q0Dn7pN ']//div[@style='margin-right: 8px;']/span[2]";
                        //    HtmlNode node = document.DocumentNode.SelectSingleNode(totalPagePath);
                        //    string sPage = node.InnerText;//共330页
                        //    int iPage = int.Parse(sPage.Replace("共", "").Replace("页", ""));
                        //    for (int i = 1; i <= iPage; i++)
                        //    {
                        //        string url = $"{rootUrl}&Page={i}";
                        //        //分别去读取每一页
                        //    }
                        //}
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {

            }
        }

        public void CrawlerElm()
        {
            HttpHelper.DownloadElm("");
        }

        /// <summary>
        /// 获取商品价格
        /// </summary>
        /// <param name="commodityList"></param>
        /// <returns></returns>
        private List<Commodity> GetCommodityPrice(Category category, List<Commodity> commodityList)
        {
            try
            {
                if (commodityList == null || commodityList.Count() == 0)
                    return commodityList;

                StringBuilder sb = new StringBuilder();
                sb.Append(@"http://p.3.cn/prices/mgets?my=list_price&type=1&area=1_72_4137&skuIds=");
                sb.Append(string.Join("%2C", commodityList.Select(c => string.Format("J_{0}", c.ProductId))));
                //
                //sb.AppendFormat("http://p.3.cn/prices/mgets?callback=jQuery1069298&type=1&area=1_72_4137_0&skuIds={0}&pdbp=0&pdtk=&pdpin=&pduid=1945966343&_=1469022843655", string.Join("%2C", commodityList.Select(c => string.Format("J_{0}", c.ProductId))));
                string html = HttpHelper.DownloadUrl(sb.ToString());
                if (string.IsNullOrWhiteSpace(html))
                {

                }
                if (html.IndexOf("(") > -1)
                    html = html.Substring(html.IndexOf("(") + 1);
                if (html.IndexOf(")") > -1)
                    html = html.Substring(0, html.LastIndexOf(")"));
                List<CommodityPrice> priceList = JsonConvert.DeserializeObject<List<CommodityPrice>>(html);
                commodityList.ForEach(c => c.Price = priceList.FirstOrDefault(p => p.id.Equals(string.Format("J_{0}", c.ProductId))).p);
                //commodityList.ForEach(c => Console.WriteLine(" Title={0}  ImageUrl={1} Url={2} Price={3} Id={4}", c.Title, c.ImageUrl, c.Url, c.Price, c.Id));
            }
            catch (Exception ex)
            {

            }
            return commodityList;
        }
    }
}
